package project.web;

import com.alibaba.fastjson.JSON;
import project.pojo.Student;
import project.service.StudentService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/registerServlet")
public class RegisterServlet extends HttpServlet {
    private StudentService service = new StudentService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        Student student = JSON.parseObject(s, Student.class);
        Student student1 = service.selectById(student.getId());

        if(student1 != null){
//             用户名存在，给出提示信息
            response.getWriter().write("unsuccess");
        }else{
            service.addStudent(student);
            response.getWriter().write("success");
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
